import 'dart:async';

import 'package:flutter/material.dart';

import '../../common/system_config.dart';
import '../../utils/tools/localstorage_tool.dart';
import '../../utils/tools/screenutil_tool.dart';
import '../../utils/widget/appbar_widget.dart';
import '../../utils/widget/toast_widget.dart';
import '../../viewmodel/userinfo_vm.dart';
import 'methods/textfield_widget.dart';

class ChangePwdView extends StatefulWidget {
  @override
  ChangePwdViewState createState() => new ChangePwdViewState();
}

class ChangePwdViewState extends State<ChangePwdView> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget("修改密码", isShowRI: false).build(context),
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: Container(
          child: Column(
            children: [
              TextfieldWidget("旧密码", 0),
              TextfieldWidget("新密码", 1),
              TextfieldWidget("确认密码", 2),
              Container(
                margin: EdgeInsets.all(ST.setw(32)),
                width: double.infinity,
                height: ST.seth(80),
                child: MaterialButton(
                  onPressed: () => changePwd(),
                  color: Theme.of(context).primaryColor,
                  child: Text("确认修改",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: SystemConfig.fs_content3
                    )
                  ),
                )
              )
            ],
          ),
        ),
      ),
    );
  }

  /// ### 确认修改密码操作
  void changePwd() async {
    // 获取输入框中的内容
    Map result = TextfieldWidget.getResult(context);

    if(result != null){
      // 执行修改密码操作
      Map res = await UserInfoVM().getNewPwd(context, result);

      // 根据返回值进行操作
      if(res != null) {
        ToastWidget.result(title: "修改成功，需要重新登录", type: true);
        Timer(Duration(seconds: 3), (){
          // 清除所有缓存
          LocalStorage.remove("userdata");

          // 跳转登录页面
          Navigator.pushNamedAndRemoveUntil(context, "/loginview", (_) => false);
        });
      }
    }
  }

}